package com.atguigu.queue;

/**
 * @author shkstart
 * @create 2021-09-23 13:37
 */
public class QueueTest {
    /**
     * 测试类
     * @author JiaoNingbo
     *
     */


        public static void main(String[] args) {

            Stack stack = new Stack();

            Node n1 = new Node(1);
            Node n2 = new Node(3);
            Node n3 = new Node(8);
            Node n4 = new Node(99);
            Node n5 = new Node(66);

            stack.push(n3);
            stack.push(n2);
            stack.push(n1);
            stack.push(n4);
            stack.push(n5);

            System.out.println(n5.toString());
            System.out.println("***************");
            Node node = stack.peek();
            System.out.println(node);
            System.out.println("***************");
            Node node2 = stack.pop();
            System.out.println(node2);
            Node node3 = stack.pop();
            System.out.println(node3);

        }
    }


/**
 * 节点
 * 在Java中实现链表是非常简单的，每个节点都有一个值，然后把它链接到下一个节点。
 * @author JiaoNingbo
 *
 */
 class Node {
    int val;        //自己的值
    Node next;    //下一个值的节点

    public Node(int val) {
        this.val = val;
        this.next = null;
    }

    @Override
    public String toString() {
        return "[val=" + val + ", next=" + next + "]";
    }

}
/**
 *
 * @author JiaoNingbo
 *
 */
 class Stack {

    Node top;   //链节点

    //返回链表
    public Node peek() {
        if (top != null) {
            return top;
        }
        return null;
    }

    //取出元素
    public Node pop() {
        if (top == null) {
            return null;
        }else {
            Node temp = new Node(top.val);
            top = top.next;
            return temp;
        }
    }

    //添加元素
    public void push(Node node) {
        if (node != null) {
            node.next = top;
            top = node;
        }
    }

}
/**
 * 队列
 * @author JiaoNingbo
 *
 */
 class Queue {

    Node first, last;

    //存
    public void enqueue(Node n) {
        if (first == null) {
            first = n;
            last = first;
        } else {
            last.next = n;
            last = n;
        }
    }

    //取
    public Node dequeue() {
        if (first == null) {
            return null;
        } else {
            Node temp = new Node(first.val);
            first = first.next;
            return temp;
        }
    }

    @Override
    public String toString() {
        return "Queue [first=" + first + ", last=" + last + "]";
    }
}

